package com.shanzmoo.db.personnel.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.exception.MallMemberException;
import com.shanzmoo.db.personnel.entity.MemberAddressEntity;
import com.shanzmoo.db.personnel.entity.dto.MemberAddressSaveDto;
import com.shanzmoo.db.personnel.entity.dto.MemberAddressUpdateDto;
import com.shanzmoo.db.personnel.entity.vo.MemberAddressVo;
import com.shanzmoo.db.personnel.mapper.MemberAddressMapper;
import com.shanzmoo.db.personnel.service.IMemberAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员收货地址表 服务实现类
 * </p>
 *
 * @author Gzhao
 * @since 2020-12-24
 */
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddressEntity> implements IMemberAddressService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addAddress(String memberId, MemberAddressSaveDto saveDto) {
        MemberAddressEntity addressEntity = saveDto.convert();
        addressEntity.setMemberId(Integer.parseInt(memberId));

        if (saveDto.getDefaultAddress()) {
            deleteDefault(memberId);
        }
        baseMapper.insert(addressEntity);
    }

    @Override
    public List<MemberAddressVo> getAddressList(String memberId) {
        List<MemberAddressEntity> list = baseMapper.selectList(
                new QueryWrapper<MemberAddressEntity>()
                        .eq("deleted", false)
                        .eq("member_id", memberId)
                        .orderByDesc("default_address")
        );

        return list.stream().map(MemberAddressVo::create).collect(Collectors.toList());
    }

    @Override
    public MemberAddressVo getDefaultAddress(Integer memberId) {
        MemberAddressEntity addressEntity = baseMapper.selectOne(
                new QueryWrapper<MemberAddressEntity>()
                        .eq("deleted", false)
                        .eq("member_id", memberId)
                        .eq("default_address", true)
        );

        return MemberAddressVo.create(addressEntity);
    }

    @Override
    public SResponse<Boolean> deleteById(Integer id, String memberId) {
        MemberAddressEntity addressEntity = obtainAddressEntityByMember(id, memberId);
        addressEntity.setDeleted(true);

        baseMapper.updateById(addressEntity);

        return SResponse.ok(Boolean.TRUE);
    }

    @Override
    public SResponse<Boolean> updateAddress(String memberId, MemberAddressUpdateDto updateDto) {
        obtainAddressEntityByMember(updateDto.getId(), memberId);

        if (updateDto.getDefaultAddress()) {
            deleteDefault(memberId);
        }

        MemberAddressEntity addressEntity = updateDto.convert();
        baseMapper.updateById(addressEntity);

        return SResponse.ok(Boolean.TRUE);

    }

    private void deleteDefault(String memberId) {
        List<MemberAddressEntity> list = baseMapper.selectList(
                new QueryWrapper<MemberAddressEntity>()
                        .eq("deleted", false)
                        .eq("member_id", memberId)
        );

        if (CollectionUtil.isNotEmpty(list)){

            list.forEach(e-> e.setDefaultAddress(false));
            this.updateBatchById(list);
        }
    }

    /**
     * 根据 地址id 获取当前用户的地址
     * @param addressId
     * @param memberId
     * @return
     */
    private MemberAddressEntity obtainAddressEntityByMember(Integer addressId, String memberId) {
        MemberAddressEntity addressEntity = baseMapper.selectById(addressId);
        if (addressEntity == null || !memberId.equals(String.valueOf(addressEntity.getMemberId()))){
            throw new MallMemberException("获取操作用户地址异常");
        }
        return addressEntity;
    }
}
